﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ZHDJ.MobileInterface
{
    public partial class ZhongGuoMengGame : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {
            // 业务逻辑返回的响应对象  
            ZhongGuoMengGameOutParam result = new ZhongGuoMengGameOutParam();
            ZhongGuoMengGameInParam param = new ZhongGuoMengGameInParam();
            string logMsg = "";
            bool isFlag = true;

            try
            {
                param = inParam as ZhongGuoMengGameInParam;

                if (string.IsNullOrEmpty(param.user_id))
                {
                    result.code = "0";
                    result.desc = "用户ID不能为空！";

                    isFlag = false;
                }
                if (isFlag)
                {
                    ///检查用户是否存在系统中
                    if (!InterfaceHelper.CheckLoginIDInSystem(param.user_id))
                    {
                        result.code = "0";
                        result.desc = "用户ID不存在，请联系管理员！";

                        isFlag = false;
                    }
                }
                bool isHave = false;
                if (isFlag)
                {
                    //检查当前用户是否有信息 true 存在 false 不存在                    
                    isHave = InterfaceHelper.checkData(param.user_id);
                    if (!isHave)
                    {
                        //insert数据  true 成功 false 不成功
                        if (!InterfaceHelper.InsertZGMData(param.user_id, param.courseId, param.progress, param.studyinfo))
                        {
                            result.code = "0";
                            result.desc = "用户数据创建失败，请联系管理员。";

                            isFlag = false;
                        }
                    }

                }
                if (isFlag)
                {
                    if (param.Type == "get")
                    {
                        DataSet ds = InterfaceHelper.Get(param.user_id);
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            Data itemtemp;
                            result.data.user_id = param.user_id;
                            foreach (DataRow dr in ds.Tables[0].Rows)
                            {
                                itemtemp = new Data();
                                itemtemp.studyInfo = ds.Tables[0].Rows[0]["studyInfo"].ToString();
                                itemtemp.courseId = ds.Tables[0].Rows[0]["courseId"].ToString();
                                itemtemp.progress = ds.Tables[0].Rows[0]["progress"].ToString();
                                result.data.list.Add(itemtemp);
                            }
                        }
                        else
                        {
                            result.code = "0";
                            result.desc = "该用户尚未创建数据，请联系管理员！";

                            isFlag = false;
                        }
                    }
                    else if (param.Type == "set")
                    {
                        //update数据 true 成功 false 不成功
                        if (!InterfaceHelper.UpdateZGMData(param.user_id, param.courseId, param.progress, param.studyinfo))
                        {
                            result.code = "0";
                            result.desc = "用户数据更新失败，请联系管理员。";

                            isFlag = false;
                        }
                    }
                    else
                    {
                        result.code = "0";
                        result.desc = "类型错误，请联系管理员。";

                        isFlag = false;
                    }
                }
                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "操作异常，多次出现请联系管理员";
                logMsg = ex.Message + ":" + ex.StackTrace;
            }

            CommonUtils.InsertMobileInterfaceLog(SystemCode + "-ZhongGuoMengGame", SerializeHelper.JsonSerializer<ZhongGuoMengGameInParam>(param), logMsg);
            return result as OutParam;
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<ZhongGuoMengGameInParam, ZhongGuoMengGameOutParam>();
            else
                SafeProcessRequest<ZhongGuoMengGameInParam, ZhongGuoMengGameOutParam>();
        }
    }

    #region 请求参数
    public class ZhongGuoMengGameInParam
    {
        /// <summary>
        /// 类型
        /// </summary>
        public string Type { get; set; }

        /// <summary>
        ///	课程学习信息
        /// </summary>
        public string studyinfo
        { get; set; }

        /// <summary>
        ///	课程进度
        /// </summary>
        public string progress
        { get; set; }

        /// <summary>
        /// 课程ID
        /// </summary>
        public string courseId
        { get; set; }

        /// <summary>
        /// 用户ID
        /// </summary>
        public string user_id
        { get; set; }
    }
    #endregion

    #region 结果参数
    public class ZhongGuoMengGameOutParam
    {
        public ZhongGuoMengGameOutParam()
        {
            code = "1";
            desc = "操作成功";
            data = new DataList();
        }

        /// <summary>
        /// 返回结果 具体见接口返回对照码 1	成功 0	失败 2	请求非法 加密结果和传入的MD5字符串进行比较，如果相同则执行接口操作，否则返回1001错误响应码
        /// </summary>
        public string code
        { get; set; }

        /// <summary>
        /// 结果描述 成功和失败都需要
        /// </summary>
        public string desc
        { get; set; }

        /// <summary>
        /// 数据集
        /// </summary>
        public DataList data
        { get; set; }
    }

    public class DataList
    {
        /// <summary>
        /// 用户ID
        /// </summary>
        public string user_id
        { get; set; }

        public DataList()
        {
            list = new List<Data>();
        }

        public List<Data> list { get; set; }
    }
    /// <summary>
    /// 数据
    /// </summary>
    public class Data
    {
        /// <summary>
        /// 课程学习信息
        /// </summary>
        public string studyInfo
        { get; set; }

        /// <summary>
        /// 课程ID
        /// </summary>
        public string courseId
        { get; set; }

        /// <summary>
        /// 课程进度
        /// </summary>
        public string progress
        { get; set; }
    }
    #endregion
}